#region Microsoft Public License
/*
 * (c) Copyright Zhun Qu, Chenjun Wu. http://utablesdk.codeplex.com
 * 
 * This source is subject to the Microsoft Public License (Ms-PL).
 * Please see http://go.microsoft.com/fwlink/?LinkID=131993 for details.
 * All other rights reserved.
 */
#endregion

using System;
using System.Windows;
using System.Windows.Media;

namespace UTable.Objects.Shapes
{
    public class URectangle: UShape
    {
        #region Dependency Properties
        public int X
        {
            get { return (int)GetValue(XProperty); }
            set { SetValue(XProperty, value); }
        }
        public static readonly DependencyProperty XProperty =
            DependencyProperty.Register("X", typeof(int), typeof(URectangle));

        public int Y
        {
            get { return (int)GetValue(YProperty); }
            set { SetValue(YProperty, value); }
        }
        public static readonly DependencyProperty YProperty =
            DependencyProperty.Register("Y", typeof(int), typeof(URectangle));


        public new int Width
        {
            get { return (int)GetValue(WidthProperty); }
            set { SetValue(WidthProperty, value); }
        }
        public static new readonly DependencyProperty WidthProperty =
            DependencyProperty.Register("Width", typeof(int), typeof(URectangle));


        public new int Height
        {
            get { return (Int32)GetValue(HeightProperty); }
            set { SetValue(HeightProperty, value); }
        }
        public static new readonly DependencyProperty HeightProperty =
            DependencyProperty.Register("Height", typeof(int), typeof(URectangle));

        #endregion

        #region Overried Methods

        protected override Geometry DefiningGeometry
        {
            get 
            {
                // Create a StreamGeometry for describing the shape
                StreamGeometry geometry = new StreamGeometry();
                geometry.FillRule = FillRule.EvenOdd;

                using (StreamGeometryContext context = geometry.Open())
                {
                    InternalDrawGeometry(context);
                }

                // Freeze the geometry for performance benefits
                geometry.Freeze();

                return geometry;
            }
        }
        /// <summary>
        /// Draws a rectangle
        /// </summary>
        private void InternalDrawGeometry(StreamGeometryContext context)
        {
            Point pt1 = new Point(X, Y);
            Point pt2 = new Point(X + Width, Y);
            Point pt3 = new Point(X + Width, Y + Height);
            Point pt4 = new Point(X, Y + Height);
            context.BeginFigure(pt1, true, true);
            context.LineTo(pt2, true, true);
            context.LineTo(pt3, true, true);
            context.LineTo(pt4, true, true);
        }

        #endregion
    }
}
